package com.ibm.law.ui.system.setup;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;

import com.ibm.law.domain.FileTypeList;
import com.ibm.law.service.system.FileTypeService;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.beans.BeansObservables;
import com.ibm.law.domain.FileType;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.jface.databinding.viewers.CellEditorProperties;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.core.databinding.beans.IBeanValueProperty;
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.action.Action;

public class TestFileType extends ApplicationWindow {
	private DataBindingContext m_bindingContext;
	private Table table;
	private FileTypeList fileTypeList=new FileTypeList();
	private TableViewer tableViewer;
	private TableViewerColumn tableViewerColumn_1;
	private TableViewerColumn tableViewerColumn;
	private Action action;
	private Action action_1;
	/**
	 * Create the application window.
	 */
	public TestFileType() {
		super(null);
		FileTypeService fileTypeService=new FileTypeService();
		fileTypeList.setList(fileTypeService.getAll());
		createActions();
		addToolBar(SWT.FLAT | SWT.WRAP);
		addMenuBar();
		addStatusLine();
	}

	/**
	 * Create contents of the application window.
	 * @param parent
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));
		{
			tableViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
			table = tableViewer.getTable();
			table.setHeaderVisible(true);
			{
				tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
				TableColumn tblclmnNewColumn = tableViewerColumn_1.getColumn();
				tblclmnNewColumn.setWidth(100);
				tblclmnNewColumn.setText("New Column");
			}
			{
				tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
				TableColumn tblclmnNewColumn_1 = tableViewerColumn.getColumn();
				tblclmnNewColumn_1.setWidth(100);
				tblclmnNewColumn_1.setText("New Column");
			}
		}
		m_bindingContext = initDataBindings();

		return container;
	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
		{
			action = new Action("Add") {

				@Override
				public void run() {
					FileType f=new FileType();
					fileTypeList.addFileType(f);
					tableViewerColumn.getViewer().editElement(f, 0);
				}				
			};
		}
		{
			action_1 = new Action("Save") {
				@Override
				public void run() {

					for(FileType f:fileTypeList.getList()){
						System.out.println(f);
					}
				}
				
			};
		}
	}

	/**
	 * Create the menu manager.
	 * @return the menu manager
	 */
	@Override
	protected MenuManager createMenuManager() {
		MenuManager menuManager = new MenuManager("menu");
		return menuManager;
	}

	/**
	 * Create the toolbar manager.
	 * @return the toolbar manager
	 */
	@Override
	protected ToolBarManager createToolBarManager(int style) {
		ToolBarManager toolBarManager = new ToolBarManager(style);
		toolBarManager.add(action);
		toolBarManager.add(action_1);
		return toolBarManager;
	}

	/**
	 * Create the status line manager.
	 * @return the status line manager
	 */
	@Override
	protected StatusLineManager createStatusLineManager() {
		StatusLineManager statusLineManager = new StatusLineManager();
		return statusLineManager;
	}

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					TestFileType window = new TestFileType();
					window.setBlockOnOpen(true);
					window.open();
					Display.getCurrent().dispose();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Configure the shell.
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("New Application");
	}

	/**
	 * Return the initial size of the window.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap[] observeMaps = BeansObservables.observeMaps(listContentProvider.getKnownElements(), FileType.class, new String[]{"name", "code"});
		tableViewer.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
		tableViewer.setContentProvider(listContentProvider);
		//
		IObservableList listFileTypeListObserveList = BeanProperties.list("list").observe(fileTypeList);
		tableViewer.setInput(listFileTypeListObserveList);
		//
		CellEditor cellEditor = new TextCellEditor(tableViewer.getTable());
		IValueProperty cellEditorProperty = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty = BeanProperties.value("name");
		tableViewerColumn_1.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor, cellEditorProperty, valueProperty));
		//
		CellEditor cellEditor_1 = new TextCellEditor(tableViewer.getTable());
		IValueProperty cellEditorProperty_1 = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty_1 = BeanProperties.value("code");
		tableViewerColumn.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor_1, cellEditorProperty_1, valueProperty_1));
		//
		return bindingContext;
	}
}
